﻿#include "data_type.h"

inline int min (int a, int b) { return (a > b ? b : a); }
inline int max (int a, int b) { return (a > b ? a : b); }
MatrixObject::MatrixObject(Matrix const* _pMatrix, int _start_row, int _start_col, int _end_row, int _end_col) 
	: Matrix(_end_row - _start_row + 1, _end_col - _start_col + 1), 
	pMatrix(_pMatrix)
{
	start_row = _start_row;
	start_col = _start_col;
	end_row = _end_row;
	end_col = _end_col;

	for(int i = 0; i < _end_col - _start_col + 1; i ++)
	{
		for(int j = 0; j < _end_row - _start_row + 1; i ++)
		{
			data_list[i][j] = _pMatrix->data_list[i + _start_col][j + _end_col];
		}
	}
}
MatrixObject::MatrixObject(MatrixObject const* object) 
	: Matrix(object->row_size, object->col_size), pMatrix(object->pMatrix)
{
	start_row = object->start_row;
	start_col = object->start_col;
	end_row = object->end_row;
	end_col = object->end_col;

	for(int i = 0; i < col_size; i ++)
	{
		for(int j = 0; j < row_size; i ++)
		{
			data_list[i][j] = object->data_list[i][j];
		}
	}
}
MatrixObject MatrixObject::operator+ (MatrixObject& _object)
{
	if(pMatrix == _object.pMatrix)
	{
		// 가장 넓은 행렬을 생성하게 된다.
		MatrixObject temp(pMatrix, min(start_row, _object.start_row), min(start_col, _object.start_col),
			max(end_row, _object.end_row), max(end_col, _object.end_col) );

		return temp;
	}

	throw Error::Matrix_error(1);
}

void MatrixObject::update()
{
	// 행렬의 크기가 맞지 않는다면
	if(col_size != (end_col - start_col + 1) || row_size != (end_row - start_row + 1))
	{
		resize(end_row - start_row + 1, end_col - start_col + 1);
	}
	
	for(int i = 0; i < col_size; i ++)
	{
		for(int j = 0; j < row_size; j++)
		{
			data_list[i][j] = pMatrix->element(i, j);
		}
	}
}